package com.example.ytyproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ytyproject.component.CheckComponent;
import com.example.ytyproject.config.SecurityUtil;
import com.example.ytyproject.config.SpringContextUtil;
import com.example.ytyproject.config.exception.AppException;
import com.example.ytyproject.entity.Role;
import com.example.ytyproject.entity.ShopsBase;
import com.example.ytyproject.entity.User;
import com.example.ytyproject.entity.UserRole;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.mapper.ShopsBaseMapper;
import com.example.ytyproject.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商铺基本信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-15
 */
@Service
public class ShopsBaseServiceImpl extends ServiceImpl<ShopsBaseMapper, ShopsBase> implements ShopsBaseService {

    @Autowired
    private AreaGuizhouService areaGuizhouService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;

    @Override
    public R listByPage(ShopsBase param) {
        QueryWrapper<ShopsBase> queryWrapper = new QueryWrapper<ShopsBase>();
        LambdaQueryWrapper<ShopsBase> lambda = queryWrapper.lambda();
        lambda.orderByDesc(ShopsBase::getCreateDate);
        if (!SecurityUtil.isAdmin()) {
            User user = SecurityUtil.getUser();
            lambda.eq(ShopsBase::getOperatePhone, user.getUsername());
        }
        Page<ShopsBase> page = this.page(new Page<>(param.getPageNo(), param.getPageSize()), queryWrapper);
        return R.ok(page.getRecords(), page.getTotal());
    }

    @Override
    public ShopsBase getShopByAddress(String detailAddress) {
        return this.getOne(new QueryWrapper<ShopsBase>().lambda().eq(ShopsBase::getDetail, detailAddress));
    }

    @Override
    public void addData(ShopsBase param) {
        CheckComponent.checkShopAddParam(param);
        // 通过地址编码获取完整路径
        String areaAddress = areaGuizhouService.getAreaAddress(param.getAreaCode()) + param.getAddress() + param.getShopName();
        param.setDetail(areaAddress)
                .setShopStatus("营业中")
                .setCreateBy(SecurityUtil.getUserId())
                .setUpdateBy(SecurityUtil.getUserId())
                .setCreateDate(LocalDateTime.now())
                .setUpdateDate(LocalDateTime.now());
        ShopsBase s = this.getShopByAddress(areaAddress);
        if (s != null) {
            throw new AppException("该商铺已存在，不能重复添加！");
        }
        this.save(param);
        // 分配用户及权限
        User user = new User();
        user.setUsername(param.getOperatePhone()).setMobile(param.getOperatePhone()).setPassword("123456").setAddress(param.getAddress());
        userService.addUser(user);
        Role role = roleService.findByCode("shop_mananger");
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId()).setRoleId(role.getId());
        userRoleService.save(userRole);
    }

    @Override
    public void updateData(ShopsBase param) {
        param.setUpdateBy(SecurityUtil.getUserId()).setUpdateDate(LocalDateTime.now());
        this.updateById(param);
    }

    @Override
    public R listShop() {
        User user = SecurityUtil.getUser();
        QueryWrapper<ShopsBase> queryWrapper = new QueryWrapper<ShopsBase>();
        LambdaQueryWrapper<ShopsBase> lambda = queryWrapper.lambda();
        lambda.orderByDesc(ShopsBase::getCreateDate);
        if (user.getUsername().equals("admin")) {
            // 超级管理员 查询全部
            List<ShopsBase> list = this.list();
            return R.ok(list);
        }
        lambda.eq(ShopsBase::getOperatePhone, user.getUsername());
        List<ShopsBase> list = this.list(queryWrapper);
        return R.ok(list);
    }
}
